<?php

namespace App\Service\CallingCards\Rms;

use App\Models\Common\ConfigModel;
use App\Models\FeishuCallingCard\FeishuCallingCardInvestmentEmployeeModel;
use Illuminate\Support\Facades\DB;

class InvestmentEmployeeService
{
    public static function addFormAll($all)
    {
        //删除所有数据
        FeishuCallingCardInvestmentEmployeeModel::query()->truncate();
        //新增表单数据
        $data = [];
        foreach ($all as $item) {
            $cityCodes = null;
            if (isset($item['cityCodes'])) {
                $cityCodes = json_encode($item['cityCodes'], JSON_UNESCAPED_UNICODE);
            }
            $data[] = [
                'name'               => mb_substr($item['name'] ?? '', 0, 32),
                'xiaokeUserId'       => $item['xiaokeUserId'] ?? 0,
                'source'             => mb_substr($item['source'] ?? '', 0, 32),
                'interactiveChannel' => mb_substr($item['interactiveChannel'] ?? '', 0, 32),
                'createdAt'          => time(),
                'updatedAt'          => time(),
                'public'             => intval($item['public'] ?? 0),
                'direct'             => intval($item['direct'] ?? 0),
                'cityCodes'          => $cityCodes,//专属城市 多个
                'fsUserId'           => $item['fsUserId'] ?? '',//飞书user_id
            ];
        }
        return FeishuCallingCardInvestmentEmployeeModel::insert($data);
    }

    /**
     * 单独新建一个-飞书人员入职时使用
     * @param array $item
     */
    public static function addOne(array $item)
    {
        $cityCodes = null;
        if (isset($item['cityCodes'])) {
            $cityCodes = json_encode($item['cityCodes'], JSON_UNESCAPED_UNICODE);
        }
        $data[] = [
            'name'               => mb_substr($item['name'] ?? '', 0, 32),
            'xiaokeUserId'       => $item['xiaokeUserId'] ?? ConfigModel::getXiaokeConfig(),
            'source'             => mb_substr($item['source'] ?? '内部推荐', 0, 32),
            'interactiveChannel' => mb_substr($item['interactiveChannel'] ?? '夸父名片', 0, 32),
            'createdAt'          => time(),
            'updatedAt'          => time(),
            'public'             => intval($item['public'] ?? 0),
            'direct'             => intval($item['direct'] ?? 1),
            'cityCodes'          => $cityCodes,             //专属城市 多个
            'fsUserId'           => $item['fsUserId'] ?? '',//飞书user_id
        ];
        FeishuCallingCardInvestmentEmployeeModel::query()->insert($data);
    }

    /**
     * 单独删除一个-飞书人员离职时使用
     * @param string $fsUserId
     */
    public static function deleteOne(string $fsUserId)
    {
        FeishuCallingCardInvestmentEmployeeModel::query()
                                                ->where('fsUserId', $fsUserId)
                                                ->delete();
    }

    /**
     * 列表
     *
     * @param object $params
     * @return array
     */
    public static function list(object $params): array
    {

        $pagination = FeishuCallingCardInvestmentEmployeeModel::query()
                                                              ->orderByDesc("source")
                                                              ->orderByDesc("direct")
                                                              ->orderByDesc('id')
                                                              ->paginate($params->pageSize, ['*'], 'pageNum',
                                                                  $params->pageNum);


        return [
            "list"     => collect($pagination->items())->map(
                function ($item) {
                    $item->createdAtText = $item->createdAt->format("Y-m-d H:i:s");
                    $item->updatedAtText = $item->updatedAt->format("Y-m-d H:i:s");
                    $item->cityCodes     = $item->cityCodes ? json_decode($item->cityCodes, true) : [];
                    return $item;
                }
            )->toArray(),
            "pageSize" => (int)$params->pageSize,
            "pageNum"  => (int)$params->pageNum,
            "count"    => (int)FeishuCallingCardInvestmentEmployeeModel::query()->count()
        ];
    }

    /**
     * 添加
     *
     * @param object $params
     */
    public static function insert(object $params)
    {
        FeishuCallingCardInvestmentEmployeeModel::query()->insert(array_merge(
            (array)$params,
            [
                "createdAt" => time(),
                "updatedAt" => time()
            ]
        ));
    }

    /**
     * 更新
     *
     * @param object $params
     */
    public static function update(object $params)
    {
        FeishuCallingCardInvestmentEmployeeModel::query()->where("id", $params->id)->update(array_merge(
            (array)$params,
            [
                "updatedAt" => time()
            ]
        ));
    }

    /**
     * 删除
     *
     * @param object $params
     */
    public static function del(object $params)
    {
        FeishuCallingCardInvestmentEmployeeModel::query()->where("id", $params->id)->delete();
    }
}
